package com.xiongjie.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.region.Region;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.multipart.MultipartForm;
import io.vertx.serviceproxy.ServiceException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Properties;

public class CosUtil {
    private static String COSPDFPATH="pdf/signature/";

    private static String secretid;
    private static String secretkey;
    private static String region;
    private static String savebucketname;
    private static COSClient cosclient;

    static {
        getProperties();
        getCOSClient();
    }

    private static void getProperties() {
        Properties pro = new Properties();
        InputStream in = CosUtil.class.getClassLoader().getResourceAsStream("cos.properties");
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        secretid = pro.getProperty("SecretId");
        secretkey = pro.getProperty("SecretKey");
        region = pro.getProperty("Region");
        savebucketname = pro.getProperty("SaveBucketName");
    }

    private static void getCOSClient() {
        COSCredentials cred = new BasicCOSCredentials(secretid, secretkey);
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        cosclient = new COSClient(cred, clientConfig);
    }

    public static void upload(Vertx vertx, String pdfPath, Handler<AsyncResult<String>> resultHandler){
        WebClient client = WebClient.create(vertx);

        File file = new File(pdfPath);
        String name = file.getName();
        String cosPath=COSPDFPATH+name;
        Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
        URL urlObj = cosclient.generatePresignedUrl(savebucketname, cosPath, expirationTime, HttpMethodName.PUT);
        cosclient.shutdown();
        String url = urlObj.toString();

        MultipartForm form = MultipartForm.create().binaryFileUpload("fileName", cosPath, file.getAbsolutePath(), "application/pdf");
        client.putAbs(url).sendMultipartForm(form, ar -> {
            if (ar.succeeded() && ar.result().statusCode()==200) {
                System.out.println("上传文件成功。cos上的路径为："+cosPath);

                resultHandler.handle(Future.succeededFuture(cosPath));
            } else {
                String error="错误码是"+ar.result().statusCode()+"上传异常："+ar.result().statusMessage();
                System.out.println(error);
                resultHandler.handle(ServiceException.fail(ar.result().statusCode(),ar.result().statusMessage(),new JsonObject()));
            }
        });
    }

}
